package com.hongmeng.teamwork.tempalte;

import com.hongmeng.teamwork.build.TaskMessageCardBuilder;
import com.hongmeng.teamwork.constants.TaskMessageConstant;
import com.hongmeng.teamwork.enums.TaskStatusEnum;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.stream.Collectors;

public class MessageSendTemplate {

    public static String createProjectTemplate(String username, String projectName, String status) {
        return new TaskMessageCardBuilder()
                .open()
                    .buildHeader(username, "创建了项目")
                    .and()
                    .openBody()
                        .buildTag("项目名", projectName).and()
                        .buildTag("负责人", username).and()
                        .buildTag("状态", status)
                    .closeBody()
                .close()
                .build();
    }

    public static String createTaskTemplate(String createdBy,
                                            String taskName,
                                            String projectName,
                                            String leaderUser,
                                            String taskDescription,
                                            TaskStatusEnum taskStatus) {
        return taskTemplate(createdBy,
                TaskMessageConstant.CREATE_TASK_MSG,
                taskName,
                projectName,
                null,
                leaderUser,
                taskDescription,
                null,
                null,
                taskStatus);
    }

    public static String createTaskTemplate(String createdBy,
                                            String taskName,
                                            String projectName,
                                            String leaderUser,
                                            String taskDescription,
                                            List<LocalDateTime> startDateTimeList,
                                            List<LocalDateTime> endDateTimeList,
                                            TaskStatusEnum taskStatus) {
        return taskTemplate(createdBy,
                TaskMessageConstant.CREATE_TASK_MSG,
                taskName,
                projectName,
                null,
                leaderUser,
                taskDescription,
                startDateTimeList,
                endDateTimeList,
                taskStatus);
    }

    public static String updateTaskTemplate(String updatedBy,
                                            String taskName,
                                            String projectName,
                                            String leaderUser,
                                            String taskDescription,
                                            TaskStatusEnum oldTaskStatus,
                                            TaskStatusEnum newTaskStatus) {
        return taskTemplate(updatedBy,
                TaskMessageConstant.UPDATE_TASK_MSG,
                taskName,
                projectName,
                null,
                leaderUser,
                taskDescription,
                null,
                null,
                oldTaskStatus, newTaskStatus);
    }

    public static String updateTaskTemplate(String updatedBy,
                                            String taskName,
                                            String projectName,
                                            String oldLeaderUser,
                                            String leaderUser,
                                            String taskDescription,
                                            List<LocalDateTime> startDateTimeList,
                                            List<LocalDateTime> endDateTimeList,
                                            TaskStatusEnum oldTaskStatus,
                                            TaskStatusEnum newTaskStatus) {
        if (oldLeaderUser.equals(leaderUser)) {
            oldLeaderUser = null;
        }
        return taskTemplate(updatedBy,
                TaskMessageConstant.UPDATE_TASK_MSG,
                taskName,
                projectName,
                oldLeaderUser,
                leaderUser,
                taskDescription,
                startDateTimeList,
                endDateTimeList,
                oldTaskStatus, newTaskStatus);
    }

    public static String directTaskTemplate(String directedBy,
                                            String taskName,
                                            String projectName,
                                            String leaderUser) {
        return taskTemplate(directedBy,
                TaskMessageConstant.DIRECT_TASK_MSG,
                taskName,
                projectName,
                directedBy,
                leaderUser,
                null,
                null,
                null);
    }

    public static String directTaskTemplate(String directedBy,
                                            String taskName,
                                            String projectName,
                                            String leaderUser,
                                            String taskDescription,
                                            List<LocalDateTime> startDateTimeList,
                                            List<LocalDateTime> endDateTimeList,
                                            TaskStatusEnum oldTaskStatus,
                                            TaskStatusEnum newTaskStatus) {
        return taskTemplate(directedBy,
                TaskMessageConstant.DIRECT_TASK_MSG,
                taskName,
                projectName,
                directedBy,
                leaderUser,
                taskDescription,
                startDateTimeList,
                endDateTimeList,
                oldTaskStatus, newTaskStatus);
    }

    private static String taskTemplate(String createdBy,
                                            String titleMsg,
                                            String taskName,
                                            String projectName,
                                            String oldLeaderUser,
                                            String leaderUser,
                                            String taskDescription,
                                            List<LocalDateTime> startDateTimeList,
                                            List<LocalDateTime> endDateTimeList,
                                            TaskStatusEnum... taskStatus) {
        TaskMessageCardBuilder taskMessageCardBuilder = new TaskMessageCardBuilder()
                .open()
                .buildHeader(createdBy, titleMsg)
                .and()
                .openBody()
                .buildTag("标题", taskName)
                .and()
                .buildTag("项目/任务", projectName + "/任务");
        if (StringUtils.isNotEmpty(oldLeaderUser)) {
            taskMessageCardBuilder.and()
                    .buildTag(TaskMessageConstant.LEAD_USER, "~~" + oldLeaderUser + "~~ -> " + leaderUser);
        } else {
            taskMessageCardBuilder.and()
                    .buildTag(TaskMessageConstant.LEAD_USER, leaderUser);
        }
        if (StringUtils.isNotBlank(taskDescription)) {
            taskMessageCardBuilder
                    .and()
                    .buildTag("任务描述", taskDescription);
        }
        // 校验是否有开始时间/截止时间
        verifyLocalDate(TaskMessageConstant.START_TIME, startDateTimeList, taskMessageCardBuilder);
        verifyLocalDate(TaskMessageConstant.END_TIME, endDateTimeList, taskMessageCardBuilder);
        // 校验拼接任务状态
        verifyTaskStatus(taskMessageCardBuilder, taskStatus);
        taskMessageCardBuilder.closeBody().close();
        return taskMessageCardBuilder.build();
    }

    private static void verifyLocalDate(String title, List<LocalDateTime> localDateTimeList,
                                            TaskMessageCardBuilder taskMessageCardBuilder) {
        if (CollectionUtils.isEmpty(localDateTimeList)) {
            return;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String message;
        if (localDateTimeList.size() >= 2) {
            message = "~~" + localDateTimeList.get(0).format(dateTimeFormatter) + "~~ -> " + localDateTimeList.get(1);
        } else {
            message = localDateTimeList.get(0).format(dateTimeFormatter);
        }
        taskMessageCardBuilder.and().buildTag(title, message);
    }

    private static void verifyTaskStatus(TaskMessageCardBuilder taskMessageCardBuilder,
                                            TaskStatusEnum[] taskStatus) {
        if (Objects.isNull(taskStatus) || taskStatus.length < 1 ) return;
        /*if (taskStatus.length > 2) {
            throw new IllegalArgumentException("taskStatus 的长度不能小于1且大于2");
        }*/
        String taskStatusMsg;
        if (taskStatus.length == 1 || taskStatus[0] == taskStatus[1]) {
            taskStatusMsg = taskStatus[0].getDesc();
        } else {
            taskStatusMsg = "~~" + taskStatus[0].getDesc() + "~~ -> " + taskStatus[1].getDesc();
        }
        taskMessageCardBuilder.and()
                .buildTag("任务状态", taskStatusMsg);
    }
}
